<?php
/**
 * Created by PhpStorm.
 * User: DELL
 * Date: 2017/10/9
 * Time: 16:33
 */
namespace Common\Model;
use Think\Model;
class UserModel extends Model{
    protected $insertFields = array('account','password','realname','admin');
    protected $updateFields = array('account','password','realname','admin');
    public $_login_validate = array(
        array('account', 'require', '用户名称不能为空！', 1),
        array('password', 'require', '密码不能为空！', 1)
    );
    /*管理员登录相关验证*/
    public function login(){
        $account=$this->account;
        $password=$this->password;
        $user=$this->where(array('account'=>array('eq',$account)))->where(array('admin'=>array('NEQ','no')))->find();
        if($user){
            if($user['password']==md5(md5($password) . $account)){
                session('id',$user['id']);
                session('name',$user['account']);
                return true;
            }else{
                $this->error='密码输入错误!';
                return false;
            }
        }else{
            $this->error='管理员帐号不存在!';
            return false;
        }
    }
    /*获取用户列表并分页显示*/
    public function getlst(){
        $where=array();
        $where['admin']=array('NEQ','super');
        $count = $this->alias('a')->where($where)->count();// 查询满足要求的总记录数
        $Page = new \Think\Page($count, 2);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $Page->setConfig('next', '下一页');
        $Page->setConfig('prev', '上一页');
        $show = $Page->show();// 分页显示输出// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
        $list= $this->field('id,account,realname,admin')->limit($Page->firstRow . ',' . $Page->listRows)->where($where)->select();
        return array(
            'list' => $list,
            'show' => $show
        );

    }

}